package com.quick.proxy.client.handler.real;

import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPromise;
import org.springframework.stereotype.Component;

/**
 * Description: 真实代理协议出站处理程序
 * Author: 吃素不吃肉
 * Date: 2023/7/9
 *
 * @author 吃素不吃肉
 */
@Component
@Sharable
public class RealProxyProtocolOutboundHandler extends ChannelOutboundHandlerAdapter {
    @Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
        ctx.writeAndFlush(msg, promise);
        promise.addListener((channelFuture) -> {
            if (!channelFuture.isSuccess()) {
                channelFuture.cause().printStackTrace();
            }
        });
    }

    @Override
    public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
        promise.addListener((channelFuture) -> {
            if (!channelFuture.isSuccess()) {
                channelFuture.cause().printStackTrace();
            }
        });
        super.close(ctx, promise);
    }
}
